//
//  AnalyzeCollectionViewCell.swift
//  MoodDiary
//
//  Created by OVIX on 2020/12/10.
//

import UIKit

class AnalyzeCollectionViewCell: UICollectionViewCell {
    
    @IBOutlet weak var numLabel: UILabel!
    @IBOutlet weak var lineView: UIView!
    @IBOutlet weak var imgV: UIImageView!
    @IBOutlet weak var bgView: UIView!
    
    var maxInt: Int = 0
    
    var orgHeight: CGFloat = 10.0
    let labelHeight: CGFloat = 20.0
    
    var colorOne: UIColor = UIColor.white
    var colorTwo: UIColor = UIColor.white
    
    var moodDic: [String: Int]? {
        didSet {
//            imgV.image = UIImage(named: moodDic?.keys.first ?? "")
            let imageText = moodDic?.keys.first ?? ""
            if let showImg = UIImage(named: imageText) {
                imgV.image = showImg
            } else {
                if var imgArray = UserDefaults.standard.array(forKey: KUSERFeelingImagesArrName) as? [[String:Any]] {
                    
                    if let imgDeletedArray = UserDefaults.standard.array(forKey: KUSERFeelingDeletedImagesArrName) as? [[String:Any]] {
                        
                        imgArray.append(contentsOf: imgDeletedArray)
                    }
                    let imageName = imgArray.first(where: { $0["text"] as! String == imageText })?["imgName"] as? String ?? ""
                    imgV.image = UIImage(named: imageName)
                }
            }
            
            numLabel.text = "\(moodDic?.values.first ?? 0)"
        }
    }
    
    var weatherDic: [String: Int]? {
        didSet {
            imgV.image = UIImage(named: weatherDic?.keys.first ?? "")
            numLabel.text = "\(weatherDic?.values.first ?? 0)"
        }
    }
    
    var analyzeType: AnalyzeCellType?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        bgView.layer.cornerRadius = bgView.width * 0.5
    }
    
    override func layoutSubviews() {
        
        bgView.frame = CGRect(x: 10.0, y: 0, width: width - 10.0 * 2, height: height - (width - 5.0 * 2) - 10.0)
        
        orgHeight = bgView.width
        
        lineView.frame = CGRect(x: 0, y: bgView.height - orgHeight, width: bgView.width, height: orgHeight)
        numLabel.frame = CGRect(x: 0, y: bgView.height - lineView.height - labelHeight, width: bgView.width, height: labelHeight)
        
        bgView.layer.cornerRadius = bgView.width * 0.5
        lineView.layer.cornerRadius = lineView.width * 0.5
        lineView.layer.masksToBounds = true
        
        var endHeight = orgHeight
        
        if analyzeType == .moodAnalyze {
            colorOne = UIColor.hexColor(hex: "#FEDA77")
            colorTwo = UIColor.hexColor(hex: "#FFEAB5")
        } else if analyzeType == .weatherAnalyze {
            colorOne = UIColor.hexColor(hex: "#67D0FF")
            colorTwo = UIColor.hexColor(hex: "#B9E4FF")
        } else {
            colorOne = UIColor.white
            colorTwo = UIColor.white
        }
        
        if analyzeType == .moodAnalyze && maxInt != 0 {

            let maxHeight = bgView.height - labelHeight
            endHeight = CGFloat(moodDic?.values.first ?? 0) / (CGFloat(maxInt) / maxHeight)

            if endHeight == 0 {
                endHeight = orgHeight
            }

            lineView.frame = CGRect(x: 0, y: bgView.height - endHeight, width: bgView.width, height: endHeight)
            numLabel.frame = CGRect(x: 0, y: bgView.height - endHeight - labelHeight, width: bgView.width, height: labelHeight)
            
        } else if analyzeType == .weatherAnalyze && maxInt != 0  {
            
            let maxHeight = bgView.height - labelHeight
            endHeight = CGFloat(weatherDic?.values.first ?? 0) / (CGFloat(maxInt) / maxHeight)
            
            if endHeight == 0 {
                endHeight = orgHeight
            }
            
            lineView.frame = CGRect(x: 0, y: bgView.height - endHeight, width: bgView.width, height: endHeight)
            numLabel.frame = CGRect(x: 0, y: bgView.height - endHeight - labelHeight, width: bgView.width, height: labelHeight)
            
        }
        
        let colors = [colorOne.cgColor, colorTwo.cgColor]
        let gradient:CAGradientLayer = CAGradientLayer()
        gradient.startPoint = CGPoint.init(x: 0, y: 0.5)
        gradient.endPoint = CGPoint.init(x: 1, y: 0.5)
        gradient.colors = colors
        gradient.frame = CGRect(x: 0, y: 0, width: bgView.width, height: endHeight)
        lineView.layer.insertSublayer(gradient, at: 0)
        
    }
    
}

